<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-tickets"></i>用户列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button> -->
                <el-input v-model="phone" placeholder="用户手机号" class="handle-input mr10"></el-input>
                <el-input v-model="nickName" placeholder="名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" class="add-user" @click="addUserClick">添加用户</el-button>
            </div>
            <el-table :data="userData" border style="width: 100%" ref="multipleTable">
                <!-- <el-table-column type="selection" width="40"></el-table-column> -->
                <el-table-column prop="id" label="编号/名称" sortable align="center">
                </el-table-column>
                <el-table-column prop="nickName" label="名称" align="center">
                </el-table-column>
                <el-table-column prop="phone" label="手机" align="center" >
                </el-table-column>
                <el-table-column prop="sponsorCount" label="发起抽奖次数" align="center">
                </el-table-column>
                <el-table-column prop="lotteryCount" label="参与抽奖次数" align="center">
                </el-table-column>
                <el-table-column prop="wonCount" label="中奖次数" align="center">
                </el-table-column>
                <el-table-column prop="openId" label="openId" align="center">
                </el-table-column>
                <el-table-column prop="createTime" label="注册时间" align="center" :formatter="createTime">
                </el-table-column>
                <el-table-column prop="status" label="状态" align="center" :formatter="status">
                </el-table-column>
                <el-table-column label="操作" width="180" fixed="right" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="see(scope.$index, scope.row)">查看</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>

            <!-- 添加系统用户 -->
        <el-dialog title="添加用户" :visible.sync="addSystemVisible" width="50%" center>
            <el-form :model="addUserForm"  status-icon :rules="addUserRules"  ref="addUserForm" label-width="200px" class="demo-ruleForm">
                <el-form-item label="openId" prop="openId">
                    <el-input type="text" v-model="addUserForm.openId" class="handle-input mr10" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="nickName">
                    <el-input type="text" v-model="addUserForm.nickName" class="handle-input mr10" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="头像" prop="activityPrizeImgUrl">
                    <el-upload
                        action="https://lottery.yhhvip.shop/yhh-lottery/home/upload/img"
                        list-type="picture-card"
                        name="file"
                        :show-file-list="true"
                        :limit=limit
                        :on-success="uploadSuccess"
                        :on-exceed="uploadExceed">
                    <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('addUserForm')">提交</el-button>
                    <el-button @click="resetForm('addUserForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        </div>
    </div>
</template>

<script>
import { Message } from 'element-ui';
import { getUserData,addUser } from '../../components/api/user.js';
import { formatDate } from '../../components/util/time.js';
    export default {
        name: 'user',
        data() {
            return {
                userData:[],
                phone: '',
                nickName:'',
                pageSize:10,
                pageNum:1,
                limit:1,
                currentPage:1,
                total:0,
                addSystemVisible:false,
                addUserForm:{
                        openId:'',
                        nickName:'',
                        headUrl:''
                },
                addUserRules:{
                    openId:[
                        {required:true,message:'请填写openId',trigger:'blur'}
                    ],
                    nickName:[
                        {required:true,message:'请填写昵称',trigger:'blur'}
                    ]
                }
            }
        },
        created(){
            this.getData();
        },
        computed: {
        },
        methods: {

            //选择列表数据条数
            handleSizeChange(val) {
                console.log('每页'+val+'条数');
                this.pageSize = val;
                this.getData();
            },
            //选择列表数据分页
            handleCurrentChange(val) {
                console.log('当前页'+val);
                this.pageNum = val;
                this.getData();
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                let router = this.$router;
                let params = {
                    nickName:this.nickName,
                    pageNum:this.pageNum,
                    pageSize:this.pageSize
                };

                //获取列表数据
               getUserData(params).then((res)=>{
                   if(res.status == 200){
                       this.userData = res.data.list;
                       this.total = res.data.total
                   }
                   console.log(res);
               }).catch((err)=>{
                   console.log(err);
               });
            },

            //列表搜索功能
            search() {
                this.getData();
            },

            createTime(row, column) {
                let date = new Date(row.createTime);
                return formatDate(date,'yyyy-MM-dd hh:mm:ss');
            },
             status(row,column){
                if(row.status == 1){
                    return '可用'
                }else {
                    return '禁用'
                }
            },            

            //查看详情
            see(index, row) {
                console.log(index);
                console.log(row.id);
                this.$router.push({name:'userDetails',params:{id:row.id}});
                
            },
        
            //添加
            addUserClick(){
                this.addSystemVisible = true;
            },

            //上传图片
            uploadSuccess(file){
                 if(file.status == 200){
                    this.addUserForm.headUrl = file.data.domain+file.data.url;
                    Message({
                        message:'上传成功',
                        type:'success'
                    });

                }
            },

            //上传图片的限制
            uploadExceed(file,fileList){
                if(file){
                    Message({
                        message:'只能上传1张图片',
                        type:'warning'
                    });
                }
            },

            //提交表单
            submitForm(formName){
                this.$refs[formName].validate((valid)=>{
                    console.log(valid);
                    if(valid){
                        let params = {
                            openId:this.addUserForm.openId,
                            nickName:this.addUserForm.nickName,
                            headUrl:this.addUserForm.headUrl
                        }
                        let that = this;
                        addUser(params).then((res)=>{
                           if(res.status == 200){
                               Message({
                                   message:'添加成功',
                                   type:'success',
                                   onClose:function(){
                                       that.addSystemVisible = false
                                   }
                               });
                           }
                        }).catch((err)=>{
                            console.log(err);
                            Message.error(err.msg);
                        });
                    }
                });
            }
        }
    }

</script>

<style scoped>
    .add-user{
        float:right;
    }
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 130px;
    }

    .handle-input {
        width: 213px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .handle-botton{
        margin: 0 0 20px 80px;
    }
</style>
